
#include "CreateQuery.h"

#pragma comment(lib, "DBFileIO.lib")



CreateQuery::CreateQuery(void)
{

}

CreateQuery::CreateQuery(const QueryInfo & q) : SQLQuery(q.type)
{
	this->SetTablename(q.table_name[0]);
	queryInfo = q;
}

RowSet CreateQuery::Execute()
{
	RowSet s;
	TABLE_INFO newTable;
	newTable.tableName = queryInfo.table_name[0];
	newTable.fileName = queryInfo.table_name[0] + ".db";
	
	int size = (int)this->queryInfo.col_name.size();
	newTable.colNum = size;
	newTable.firstPageId = -1;
	newTable.tableId = (int)tableInfos.size()+1;
	COL_INFO colInfo;
	struct ColInfo* info;
	int i;
	int num = 0;
	int sum = 0;
	for(i = 0 ; i < size ; i++)
	{
		info = &this->queryInfo.col_name[i];
		colInfo.colId = i + 1;
		colInfo.xType = info->data_type;
		colInfo.length = info->data_len;
		if(colInfo.xType != 3)
		{
			num++;
			if(colInfo.xType ==1) sum += 4;
			else
				sum += colInfo.length;
		}
		colInfo.name = info->col_name;
		colInfo.nullable = info->nullable;
		colInfo.xOffset = 0;
		newTable.colInfo.push_back(colInfo);
	}
	newTable.varColNum = size - num;
	newTable.minLen = sum;
	tableInfos.push_back(newTable);
	DBCreateFile(newTable.fileName.c_str());
	return s;
}

CreateQuery::~CreateQuery(void)
{

}

